package com.yanqu.road.dao.impl.activity.huashigang;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.huashigang.HuaShiGangData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author wpj
 * @Date 2020/10/11
 */
public class HuaShiGangBaseDataDaoImpl extends TempDataObjectDao<HuaShiGangData> {
    public static String getHuaShiGangDataSql = "select * from t_c_huashigang_data where activityId=?";

    public HuaShiGangData getHuaShiGangData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getHuaShiGangDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                HuaShiGangData huaShiGangData = parse(rs);
                return huaShiGangData;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateHuaShiGangDataSql = "update t_c_huashigang_data set modeData=?,allAbility=?,totalDamage=?,bossYList=? where activityId=?";

    @Override
    protected boolean updateData(HuaShiGangData huaShiGangData) {
        huaShiGangData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, huaShiGangData.getModeData()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(huaShiGangData.getAbilityMap())));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangData.getTotalDamage()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(huaShiGangData.getBossYList())));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangData.getActivityId()));
        if (getDb().executeNoneQuery(updateHuaShiGangDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertHuaShiGangDataSql = "insert into t_c_huashigang_data(activityId,modeData,allAbility,totalDamage,bossYList) values (?,?,?,?,?)";

    @Override
    protected boolean addData(HuaShiGangData huaShiGangData) {
        huaShiGangData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, huaShiGangData.getModeData()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(huaShiGangData.getAbilityMap())));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangData.getTotalDamage()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(huaShiGangData.getBossYList())));
        if (getDb().executeNoneQuery(insertHuaShiGangDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected HuaShiGangData parseData(ResultSet rs) throws SQLException {
        HuaShiGangData huaShiGangData = new HuaShiGangData();
        huaShiGangData.setActivityId(rs.getInt("activityId"));
        huaShiGangData.setModeData(rs.getString("modeData"));
        huaShiGangData.setAbilityMap(JSONObject.parseObject(rs.getString("allAbility"), new TypeReference<Map<Long, Long>>() {}));
        huaShiGangData.setTotalDamage(rs.getLong("totalDamage"));
        huaShiGangData.setBossYList(JSONObject.parseObject(rs.getString("bossYList"), new TypeReference<List<Integer>>() {}));
        return huaShiGangData;
    }
}
